package server;

import java.rmi.*;
import javax.naming.*;

import service.Book;
import service.Product;


/**
 * This server program instantiates a remote warehouse object, registers it
 * with the naming service, and waits for clients to invoke methods.
 */
public class WarehouseServer {
    public static void main(String[] args) throws RemoteException, NamingException {
	// Instruct the security manager to read the policy file by setting
	// java.security.policy property to the file name.
	// Alternatively on the command line: -Djava.security.policy=rmi.policy
	
      //Comment if issues
        System.setProperty("java.security.policy", "server.policy");
	System.setSecurityManager(new SecurityManager());	
      //
        
	// A warehouse has a referenc to a backup warehouse.
	// If an item cannot be found in the warehouse, the backup warehouse
	// is searched.
	System.out.println("Constructing server implementation...");
	WarehouseImpl backupWarehouse = new WarehouseImpl(null);
	WarehouseImpl centralWarehouse = new WarehouseImpl(backupWarehouse);

	centralWarehouse.add("toaster", new Product("Blackwell Toaster", 23.95));
	backupWarehouse.add("java", new Book("Core Java vol. 2", "0132354799", 44.95));

	System.out.println("Binding server implementation to registry...");
	// Associates the name central_warehouse with the object referenced by centralWarehouse
	// By default the server is localhost and the port number is 1099, if not specified.
	Context namingContext = new InitialContext();
	namingContext.bind("rmi:central_warehouse", centralWarehouse);

	System.out.println("Waiting for invocations from clients...");
    }
}
